package apbms

class DependenteController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def create = {
        def dependenteInstance = new Dependente()
        dependenteInstance.properties = params
        return [dependenteInstance: dependenteInstance]
    }

    def save = {
        def dependenteInstance = new Dependente(params)
        if (dependenteInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'dependente.label', default: 'Dependente'), dependenteInstance.id])}"
            redirect(action: "show", id: dependenteInstance.id)
        }
        else {
            render(view: "create", model: [dependenteInstance: dependenteInstance])
        }
    }

    def show = {
        def dependenteInstance = Dependente.get(params.id)
        if (!dependenteInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'dependente.label', default: 'Dependente'), params.id])}"
            redirect(action: "list")
        }
        else {
            [dependenteInstance: dependenteInstance]
        }
    }

    def edit = {
        def dependenteInstance = Dependente.get(params.id)
        if (!dependenteInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'dependente.label', default: 'Dependente'), params.id])}"
            redirect(controller: "socio", action: "list")
        }
        else {
            return [dependenteInstance: dependenteInstance]
        }
    }

    def update = {
        def dependenteInstance = Dependente.get(params.id)
        if (dependenteInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (dependenteInstance.version > version) {
                    
                    dependenteInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'dependente.label', default: 'Dependente')] as Object[], "Another user has updated this Dependente while you were editing")
                    render(view: "edit", model: [dependenteInstance: dependenteInstance])
                    return
                }
            }
            dependenteInstance.properties = params
            if (!dependenteInstance.hasErrors() && dependenteInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'dependente.label', default: 'Dependente'), dependenteInstance.id])}"
                redirect(action: "show", id: dependenteInstance.id)
            }
            else {
                render(view: "edit", model: [dependenteInstance: dependenteInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'dependente.label', default: 'Dependente'), params.id])}"
            redirect(controller: "socio", action: "list")
        }
    }

    def delete = {
        if(session.usuario.grupo.equalsIgnoreCase("Visualizador")){
            redirect(action: "show", id: params.id)
        }else{
            def dependenteInstance = Dependente.get(params.id)
            if (dependenteInstance) {
                try {
                    dependenteInstance.delete(flush: true)
                    flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'dependente.label', default: 'Dependente'), params.id])}"
                    redirect(controller: "socio", action: "show", id: params.socio.id)
                }
                catch (org.springframework.dao.DataIntegrityViolationException e) {
                    flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'dependente.label', default: 'Dependente'), params.id])}"
                    redirect(action: "show", id: params.id)
                }
            }
            else {
                flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'dependente.label', default: 'Dependente'), params.id])}"
                redirect(controller: "socio", action: "list")
            }
        }
    }
}
